﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;

namespace Admin
{
    public partial class AdminData : Form
    {
        int index = 0;
        bool changed_one;
        List<Models.Admin> all_admin;
        List<Models.Admin> admin_added = new List<Models.Admin>();
        BLL.B_Admin b_Admin = new BLL.B_Admin();
        public AdminData()
        {
            InitializeComponent();
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (b_Admin.save_List(all_admin))
                MessageBox.Show("保存成功！");
        }

        private void StudentData_Load(object sender, EventArgs e)
        {
            refresh();
        }

        private void refresh()
        {
            all_admin = b_Admin.GetList();
            Admins.DataSource = new BindingList<Models.Admin>(all_admin);
        }

        private void Del_Click(object sender, EventArgs e)
        {
            List<Models.Admin> admin_deleted = new List<Models.Admin>();
            foreach(DataGridViewRow row in Admins.SelectedRows) 
            {
                admin_deleted.Add(row.DataBoundItem as Models.Admin);
            }
            if (b_Admin.Del(admin_deleted))
                MessageBox.Show("删除成功！");

            refresh();
        }

        private void Preview_Click(object sender, EventArgs e)
        {
            index = 0;

            printPreviewDialog1.Document = printDocument1;

            if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int y = 120;
            Font f = new Font("宋体", 20);

            //先移动后旋转
            e.Graphics.TranslateTransform(e.PageBounds.Width, e.PageBounds.Height);
            e.Graphics.RotateTransform(180);

            //for (int index=0 ; index < 30; index++)会出题，为什么index不在这里写初始值，没答上来扣33分？
            //{
            //    e.Graphics.DrawString(all_admin[1].admin_id.ToString(), f, Brushes.Black, 200, y);
            //    e.Graphics.DrawString(all_admin[1].admin_uname, f, Brushes.Black, 400, y);
            //    e.Graphics.DrawString(all_admin[1].pwd, f, Brushes.Black, 500, y);
            //    y += 120;
            //    if (y >= e.PageBounds.Height - 100)
            //    {
            //        index++;
            //        e.HasMorePages = true;//还有下一页要打。因为一页打八行，打到八行后，开始分页，但是index是在for循环初始化的，调用这个函数后，又从1开始打印，一直打印的是第一到第八行，一直没有超过第九行，然后就检测到是始终没有打印到第三十行，所以这个循环就不会停止。将index定义为全局变量后，打印第二页就接上从第一页的最后一行，从第九行开始打印。
            //        break;
            //    }
            //}
            //写成下面的形式是为了分页
            for (; index < all_admin.Count; index++)
            {
                e.Graphics.DrawString(all_admin[index].admin_id.ToString(), f, Brushes.Black, 200, y);
                e.Graphics.DrawString(all_admin[index].admin_uname, f, Brushes.Black, 400, y);
                e.Graphics.DrawString(all_admin[index].pwd, f, Brushes.Black, 500, y);
                y += 120;
                if (y >= e.PageBounds.Height - 100)
                {
                    index++;
                    e.HasMorePages = true;
                    break;
                }
            }
        }


        private void Admins_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 2) return;
            if (changed_one)
            {
                Admins.CurrentCell.Value = MD5Provider.Hash(Admins.CurrentCell.Value.ToString());
                changed_one = false;
            }
        }

        private void Admins_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 2) return;

            changed_one = true;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            List<Models.Admin> admin_deleted = new List<Models.Admin>();
            foreach (DataGridViewRow row in Admins.SelectedRows)
            {
                admin_deleted.Add(row.DataBoundItem as Models.Admin);
            }
            if (b_Admin.Del(admin_deleted))
                MessageBox.Show("删除成功！");

            refresh();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (b_Admin.save_List(all_admin))
                MessageBox.Show("保存成功！");
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            index = 0;

            printPreviewDialog1.Document = printDocument1;

            if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }
    }
}
